<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/10/16
 * Time: 14:12
 */

namespace app\api\model;

use think\Controller;
use think\Model;
use think\Db;
use app\api\model\HotelRoom as hr;
use app\common\model\AppraiseModel as app;

class Hotel extends Model
{
    /*
     * 首页 住啥
     */
    public function live($page, $latitude, $longitude)
    {
        $res = Db::name('hotel')
            ->field('hotel_id,hotel_name,hotel_covers,hotel_tag,longitude,latitude,sum_sale')
            ->where('is_del', 0)
            ->where('is_pass', 1)
            ->where('is_display', 0)
            ->where('is_tuijian', 1)
            ->order('sort', 'desc')
            ->limit(($page - 1) * config('page_size'), config('page_size'))
            ->select();
        foreach ($res as $k => $v) {
            $res[$k]['hotel_covers'] = addWaterByField($v['hotel_covers']);
            if (empty($v['hotel_tag'])) {
                $res[$k]['hotel_tag'] = [];
            } else {
                $res[$k]['hotel_tag'] = explode(',', $v['hotel_tag']);
            }
            //查询门票最低价
            $res[$k]['price'] = Db::name('hotel_room')
                ->field('room_price')
                ->where('hotel_id', $v['hotel_id'])
                ->where('is_display', 0)
                ->where('is_del', 0)
                ->min('room_price');
            //处理景点没上架门票的情况
            if ($res[$k]['price'] == 0) {
                $res[$k]['price'] = '暂无';
            }

            //处理好评率
            $res[$k]['appraise'] = model('AppraiseModel')->getAppRate(1, $v['hotel_id']);
            $res[$k]['range'] = getDistance($latitude, $longitude, $v['latitude'], $v['longitude']);
        }
        return $res;
    }

    /*
     * 酒店列表
     */
    public function hotel_list($where, $page, $status = '', $longitude = '', $latitude = '', $limit = '')
    {
        $res = Db::name('hotel')
            ->field('hotel_id,hotel_name,hotel_covers,hotel_tag,longitude,latitude,sum_sale')
            ->where('is_pass', 1)
            ->where('is_del', 0)
            ->where('is_display', 0)
            ->where($where)
            ->limit(($page - 1) * config('page_size'), config('page_size'))
            ->select();
        $limit = ($limit === '') ? config('page_size') : $limit;
        //php获取本月起始时间戳和结束时间戳
//        $thismonth_start=mktime(0,0,0,date('m'),1,date('Y'));
//        $thismonth_end=mktime(23,59,59,date('m'),date('t'),date('Y'));
//        $wheres['create_time'] = ['between time', [$thismonth_start,$thismonth_end]];
        //查询订单
        foreach ($res as $k => $v) {
            $res[$k]['hotel_covers'] = addWaterByField($v['hotel_covers']);
            if (empty($v['hotel_tag'])) {
                $res[$k]['hotel_tag'] = [];
            } else {
                $res[$k]['hotel_tag'] = explode(',', $v['hotel_tag']);
            }
            //查询门票最低价
            $hotelRoomInfo = Db::name('hotel_room')
                ->field('room_price,is_tehui,room_discount_price')
                ->where('hotel_id', $v['hotel_id'])
                ->where('is_display', 0)
                ->where('is_del', 0)
                ->order("room_price asc")
                ->limit(0, 1)
                ->find();

            if ($hotelRoomInfo['is_tehui'] == 1) {
                $res[$k]['price'] = $hotelRoomInfo['room_discount_price'];
            }
            if ($hotelRoomInfo['is_tehui'] == 0) {
                $res[$k]['price'] = $hotelRoomInfo['room_price'];
            }

            //处理景点没上架门票的情况
            if ($res[$k]['price'] == 0) {
                $res[$k]['price'] = '暂无';
            }

            //处理好评率 
            $res[$k]['appraise'] = model('AppraiseModel')->getAppRate(2, $v['hotel_id']);
            $res[$k]['range'] = getDistance($latitude, $longitude, $v['latitude'], $v['longitude']);
        }

        if ($status == 1) {
            //根据某个值对二维数组排序
            $last_names = array_column($res, 'sum_sale');
            array_multisort($last_names, SORT_DESC, $res);
        } else if ($status == 2) {
            $last_names = array_column($res, 'price');
            array_multisort($last_names, SORT_ASC, $res);
        } else if ($status == 3) {
            $last_names = array_column($res, 'appraise');
            array_multisort($last_names, SORT_DESC, $res);
        } else if ($status == 4) {
            $last_names = array_column($res, 'range');
            array_multisort($last_names, SORT_ASC, $res);
        }
        return $res;
    }

    /**
     * [getHotelInfo 获取酒店的详情]
     * @Author   kendy
     * @DateTime 2019-10-31T10:54:30+0800
     * @param    [type]                   $hotel_id [酒店id]
     * @return   [type]                             [description]
     */
    public function getHotelInfo($hotel_id)
    {
        $resultData = Db::name('hotel')
            ->field('hotel_id,hotel_name,city,hotel_covers,longitude,latitude,hotel_subject,hotel_tag,hotel_tel,address,should_konwn')
            ->where(['is_del' => 0, 'is_pass' => 1, 'hotel_id' => $hotel_id])
            ->find();
        if (!empty($resultData)) {
            $resultData['address'] = $resultData['city'].$resultData['address'];
            unset($resultData['city']);
            $app = new app();
            $hr = new hr();
            $resultData['hotel_tag'] = explode(',', $resultData['hotel_tag']);
            //酒店图片，权重倒序排序
            $imagsArr = Db::name('hotel_img')
                ->field('id,img_path')
                ->order('sort desc')
                ->select();
            $resultData['hotel_images'] = [];
            if (count($imagsArr) > 0) {
                foreach ($imagsArr as $key => $value) {
                    $imgArr[] = addWaterByField($value['img_path']);
                }
            }
            $resultData['hotel_images'] = $imgArr;
            //好评率
            $resultData['evaluat_rate'] = $app->getAppRate(2, $resultData['hotel_id']);
            //获取酒店的客房类型
            $resultData['room_list'] = $hr->getRoomListById($resultData['hotel_id']);
            ///获取该景点的最近一条好评
            $resultData['app'] = model('AppraiseModel')->getAppList(2, (int)$resultData['hotel_id'], 1, 1, 1);
            //评价个数
            $resultData['count'] = model('AppraiseModel')->getAppCountByItemId(2, (int)$resultData['hotel_id']);
            //附近景点
            $resultData['nearby_tours'] = model('Tour')->tour_list([], 1, 1, $resultData['longitude'], $resultData['latitude'], 3);
            //附近美食
            $resultData['nearby_food'] = model('Food')->food_list([], 1, 1, $resultData['longitude'], $resultData['latitude'], 3);

            return $resultData;
        }
        return $resultData;
    }

    /**
     *
     */
    public function insert_hotel($post)
    {
        $res = Db::name('hotel')->insertGetId($post);
        if ($res) {
            return $res;
        } else {
            return false;
        }
    }
}